package cn.edu.tju.elm.controller;

import cn.edu.tju.core.model.HttpResult;
import cn.edu.tju.core.model.User;
import cn.edu.tju.elm.dto.OrderCreateDto;
import cn.edu.tju.elm.model.Order;
import cn.edu.tju.elm.service.OrderService;
import cn.edu.tju.core.security.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import cn.edu.tju.elm.model.OrderDetailet;

@RestController
@RequestMapping("/api/orders")
@Tag(name = "管理订单", description = "对订单进行增删改查")
public class OrderController {
    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @Autowired
    public OrderController(UserService userService, OrderService orderService) {
        this.userService = userService;
        this.orderService = orderService; // 添加这一行
    }

    @PostMapping("")
    public HttpResult<Order> addOrders(@RequestBody OrderCreateDto orderDto) {
        // 获取当前登录的用户
        User currentUser = userService.getUserWithAuthorities()
                .orElseThrow(() -> new AccessDeniedException("用户未登录"));

        // 调用 Service 层创建订单
        Order newOrder = orderService.createOrder(orderDto, currentUser);
        
        return HttpResult.success(newOrder);
    }

    @GetMapping("/{id}")
    public HttpResult<Order> getOrderById(@PathVariable Long id) {
        // 获取当前登录的用户
        User currentUser = userService.getUserWithAuthorities()
                .orElseThrow(() -> new AccessDeniedException("用户未登录"));

        // 调用 Service 层的方法来获取并校验订单
        Order order = orderService.getOrderById(id, currentUser);

        return HttpResult.success(order);
    }

    @GetMapping("")
    @Operation(summary = "获取当前登录用户的所有订单列表")
    public HttpResult<List<Order>> listMyOrders() {
        // 1. 获取当前登录的用户信息
        User currentUser = userService.getUserWithAuthorities()
                .orElseThrow(() -> new AccessDeniedException("用户未登录"));

        // 2. 调用 Service 层获取该用户的所有订单
        List<Order> orders = orderService.listOrdersForUser(currentUser);

        // 3. 返回成功响应
        return HttpResult.success(orders);
    }

    @GetMapping("/business/{businessId}")
    @Operation(summary = "根据商家ID获取该商家的订单列表")
    public HttpResult<List<Order>> listOrdersByBusiness(@PathVariable Long businessId) {
        // 获取当前登录用户
        User currentUser = userService.getUserWithAuthorities()
                .orElseThrow(() -> new AccessDeniedException("用户未登录"));
        // 查询订单
        List<Order> orders = orderService.listOrdersForBusiness(businessId, currentUser);
        return HttpResult.success(orders);
    }

    @GetMapping("/{id}/detailets")
    @Operation(summary = "获取订单的商品详情")
    public HttpResult<List<OrderDetailet>> getOrderDetailets(@PathVariable("id") Long orderId) {
        // 1. 获取当前登录的用户
        User currentUser = userService.getUserWithAuthorities()
                .orElseThrow(() -> new AccessDeniedException("用户未登录"));

        // 2. 调用 Service 层的方法来获取订单详情
        List<OrderDetailet> detailets = orderService.getOrderDetailets(orderId, currentUser);

        return HttpResult.success(detailets);
    }

    @PutMapping("/{orderId}/status")
    @Operation(summary = "更新订单状态")
    public HttpResult<Order> updateOrderStatus(@PathVariable Long orderId, @RequestParam Integer status) {
        // 1. 获取当前登录的用户
        User currentUser = userService.getUserWithAuthorities()
                .orElseThrow(() -> new AccessDeniedException("用户未登录"));

        // 2. 更新订单状态
        Order updatedOrder = orderService.updateOrderStatus(orderId, status, currentUser);

        // 3. 返回成功响应
        return HttpResult.success(updatedOrder);
    }
}
